﻿#include "xlabelpixmap_p.h"

XLabelPixmapPrivate::XLabelPixmapPrivate()
    : borderColor(Qt::black),
      borderWidth(1),
      borderRadius(5),
      labelBgColor(qRgba(0,197,205,100)),
      displayPixmap(),
      displayEnable(true),
      defaultText("Pixmap"),
      pixmapBuffer(new QPixmap),
      LeftmousePressed(false),
      mousePressPoint(0, 0)
{

}

XLabelPixmapPrivate::~XLabelPixmapPrivate()
{

}

XLabelPixmap::XLabelPixmap(QWidget *parent, Qt::WindowFlags f)
    : QLabel(parent, f),
      d(new XLabelPixmapPrivate)
{
    this->setText(d->defaultText);
}

XLabelPixmap::~XLabelPixmap()
{
    delete d;
}

/*!
    \property XLabelPixmap::borderColor
    \brief the color of the border

    The border is the outermost layer of the XLabelPixmap.

    The default border is black.

    \sa indent
*/
QColor XLabelPixmap::getBorderColor() const
{
    return d->borderColor;
}

void XLabelPixmap::setBorderColor(const QColor &color)
{
    if(d->borderColor == color)
        return;
    d->borderColor = color;
    this->update();
}

/*!
    \property XLabelPixmap::borderWidth
    \brief the width of the border

    The border is the outermost layer of the XLabelPixmap.

    The default border is 0.

    \sa indent
*/
int XLabelPixmap::getBorderWidth() const
{
    return d->borderWidth;
}

void XLabelPixmap::setBorderWidth(int borderWid)
{
    if(d->borderWidth == borderWid)
        return;
    d->borderWidth = borderWid;
    this->update();
}

/*!
    \property XLabelPixmap::borderRadius
    \brief the radius of the border

    The border is the outermost layer of the XLabelPixmap.

    The default radius is 0.

    \sa indent
*/
int XLabelPixmap::getBorderRadius() const
{
    return d->borderRadius;
}

void XLabelPixmap::setBorderRadius(int borderRad)
{
    if(d->borderRadius == borderRad)
        return;
    d->borderRadius = borderRad;
    this->update();
}

/*!
    \property XLabelPixmap::labelBgColor
    \brief the color of the labelBackground

    The border is the outermost layer of the XLabelPixmap.

    The default border is white.

    \sa indent
*/
QColor XLabelPixmap::getLabelBgColor() const
{
    return d->labelBgColor;
}

void XLabelPixmap::setLabelBgColor(const QColor &color)
{
    if(d->labelBgColor == color)
        return;
    d->labelBgColor = color;
    this->update();
}

/*!
    \property XLabelPixmap::displayString
    \brief the displayString of XLabelPixmap

    True or False corresponds to display or undisplay.

    The default DisplayEnable is true.

    \sa indent
*/
QPixmap XLabelPixmap::getDisplayPixmap() const
{
    return d->displayPixmap;
}

void XLabelPixmap::setDisplayPixmap(QPixmap &pixmap)
{
    if(d->displayPixmap == pixmap)
        return;
    d->displayPixmap = pixmap;
    this->update();
}


/*!
    \property XLabelPixmap::displayEnable
    \brief control the XLabelPixmap whether or not display

    True or False corresponds to display or undisplay.

    The default DisplayEnable is true.

    \sa indent
*/
bool XLabelPixmap::getDisplayEnable() const
{
    return d->displayEnable;
}

void XLabelPixmap::setDisplayEnable(bool displayEnable)
{
    if(d->displayEnable == displayEnable)
        return;
    d->displayEnable = displayEnable;

    if(!d->displayEnable)
    {
        d->pixmapBuffer = pixmap();
        this->clear();
        this->setText(d->defaultText);
    }
    else
    {
        this->setPixmap(*d->pixmapBuffer);
    }
    this->update();
}

/*!
    \reimp
*/
void XLabelPixmap::paintEvent( QPaintEvent *ev)
{
    Q_UNUSED(ev)

    //绘制准备工作,启用反锯齿
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);

    //绘制边框
    drawBorder(&painter);
    //绘制背景
    drawBg(&painter);

//    //////////////////////////////////////////////////////////////
//    QPainter p(this);//创建画家对象

//    int size=qMin(width(),height());
//    p.setRenderHint(QPainter::Antialiasing);//防止图形走样
//    p.translate(width()/2,height()/2);//平移坐标系置中心
//    p.scale(size/600.0,size/600.0);//缩放
//    QPen pen;//定义画笔
//    pen.setWidth(18);//设置画笔宽度
//    pen.setColor(QColor(205,214,216));//rgb设置颜色
//    pen.setStyle(Qt::SolidLine);//设置风格
//    p.setPen(pen);//将画笔交给画家
//    //绘制小时线
//    pen.setStyle(Qt::SolidLine);
//    pen.setWidth(5);
//    pen.setColor(Qt::black);
//    p.setPen(pen);

//    for(int i=0;i<12;i++)
//    {
//        p.drawLine(0,268,0,276);//画小时线
//        p.drawText(-5,-235,QString::number(i));//表明小时数
//        p.rotate(30);//每画一次旋转30度
//    }

    /////////////////////////////////////////////////////////////////
//    // 扩展径向渐变(focusRadius = 50)
//    QRadialGradient radialGradient(width() / 2, height() / 2, 70, 2 * width() / 3, height() / 3, 10);

//    radialGradient.setColorAt(0, Qt::white);
//    radialGradient.setColorAt(1, Qt::green);
//    radialGradient.setSpread(QGradient::ReflectSpread);

//    QBrush brush(radialGradient);

//    painter.setBrush(brush);

//    painter.drawEllipse(10, 10, width() - 20, height() - 20);

//    // axis
//    painter.setPen(QPen(Qt::red));
//    painter.drawLine(width() / 2 - 30, height() / 2, width() / 2 + 30, height() / 2);
//    painter.drawLine(width() / 2, height() / 2 + 30, width() / 2, height() / 2 - 30);


    // 构造一个锥向渐变(center, angle)
    // 构造一个线性渐变(start, finalStop)
//     QLinearGradient linearGradient(QPointF(0, height() / 3), QPointF(0, 2 * height() / 3));
//     linearGradient.setColorAt(0, Qt::white);
//     linearGradient.setColorAt(1, Qt::black);
//     linearGradient.setSpread(QGradient::ReflectSpread);
//     // 使用线性渐变创建一个画刷，用来填充
//     QBrush brush(linearGradient);

//    painter.setBrush(brush);

//    painter.drawRect(0, 0, width(), height());

//   painter.setBrush(Qt::black);

//   painter.drawRect(0, 2 * height() / 3, width(), height());



    QLabel::paintEvent(ev);
}

/*!
 * \brief XLabelPixmap::drawBorder
 * \param painter
 */
void XLabelPixmap::drawBorder(QPainter *painter)
{
    painter->save();

    painter->setPen(QPen(d->borderColor, d->borderWidth));
    painter->setBrush(Qt::NoBrush);

    QPointF topLeft(0, 0);
    QPointF bottomRight(width(), height());
    QRectF rect(topLeft, bottomRight);

    painter->drawRoundedRect(rect, d->borderRadius, d->borderRadius);

    painter->restore();
}

/*!
 * \brief XLabelPixmap::drawBg
 * \param painter
 */
void XLabelPixmap::drawBg(QPainter *painter)
{
    painter->save();

    QPointF topLeft(this->margin(), this->margin());
    QPointF bottomRight(width() - this->margin(), height() - this->margin());
    QRectF bgRect(topLeft, bottomRight);
    QBrush bgBrush(d->labelBgColor);
    painter->setPen(Qt::NoPen);
    painter->setBrush(bgBrush);
    painter->drawRoundedRect(bgRect, d->borderRadius/2, d->borderRadius/2);

    painter->restore();
}

/*!
 * \brief XLabelPixmap::enterEvent
 * \param ev
 */
void XLabelPixmap::enterEvent(QEvent *ev)
{
    if(QEvent::Enter == ev->type())
    {
        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::PointingHandCursor);  //设置光标形状(指)
        setCursor(cursor);                        //使用光标
    }
}

/*!
 * \brief XLabelPixmap::leaveEvent
 * \param ev
 */
void XLabelPixmap::leaveEvent(QEvent *ev)
{
    if(QEvent::Leave == ev->type())
    {
        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::ArrowCursor);         //设置光标形状(箭头)
        setCursor(cursor);                        //使用光标
    }
}

/*!
 * \brief XLabelPixmap::mousePressEvent
 * \param ev
 */
void XLabelPixmap::mousePressEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        d->LeftmousePressed = true;
        d->mousePressPoint = ev->globalPos();

        QCursor cursor;                         //创建光标对象
        cursor.setShape(Qt::ClosedHandCursor);  //设置光标形状(抓)
        setCursor(cursor);                      //使用光标
    }
}

/*!
 * \brief XLabelPixmap::mouseReleaseEvent
 * \param ev
 */
void XLabelPixmap::mouseReleaseEvent(QMouseEvent *ev)
{
    if(Qt::LeftButton == ev->button())
    {
        d->LeftmousePressed = false;

        QCursor cursor;                           //创建光标对象
        cursor.setShape(Qt::PointingHandCursor);  //设置光标形状(指)
        setCursor(cursor);                        //使用光标
    }
}

/*!
 * \brief XLabelPixmap::mouseMoveEvent
 * \param ev
 */
void XLabelPixmap::mouseMoveEvent(QMouseEvent *ev)
{
    // 鼠标左键拖动移动
    if(d->LeftmousePressed)
    {
        double mouseDx = ev->globalPos().x() - d->mousePressPoint.x();
        double mouseDy = ev->globalPos().y() - d->mousePressPoint.y();
        d->mousePressPoint = ev->globalPos();

        double myLabelNewx = this->x() + mouseDx;
        double myLabelNewy = this->y() + mouseDy;

        if(myLabelNewx < 0)
            myLabelNewx = 0;
        if(myLabelNewx + this->width() > QApplication::activeWindow()->width())
            myLabelNewx = QApplication::activeWindow()->width() - this->width();
        if(myLabelNewy < 0)
            myLabelNewy = 0;
        if(myLabelNewy + this->height() > QApplication::activeWindow()->height())
            myLabelNewy = QApplication::activeWindow()->height() - this->height();

        move(myLabelNewx, myLabelNewy);
        update();
    }
}
